package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.query.ClientObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {
    @Autowired
    private IClientService clientService;

    @RequiresPermissions("client:index")
    @PermissionName("客户主页")
    @RequestMapping("index")
    public String index(@ModelAttribute("qo") ClientObjectQuery qo) {
        return "client/list";
    }

    @RequiresPermissions("client:list")
    @PermissionName("客户查询")
    @RequestMapping("list")
    @ResponseBody
    public PageResult<Client> list(@ModelAttribute("qo") ClientObjectQuery qo) {
        PageResult<Client> list = clientService.query(qo);
        return list;
    }

    @RequestMapping("query")
    @ResponseBody
    public List<Client> query() {
        return clientService.getAll();
    }

    @RequiresPermissions("client:delete")
    @PermissionName("客户删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                clientService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除客户失败");
        }
        return jsonResult;
    }

    @RequiresPermissions("client:save")
    @PermissionName("客户创建")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Client client) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.save(client);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("创建客户失败");
        }
        return jsonResult;
    }

    @RequiresPermissions("client:update")
    @PermissionName("客户更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Client client) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.update(client);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("客户信息修改失败,请重试");
        }
        return jsonResult;
    }

    @RequiresPermissions("client:changeClientState")
    @PermissionName("客户状态改变")
    @RequestMapping("changeClientState")
    @ResponseBody
    public JsonResult changeClientState(Client client) {
        JsonResult jsonResult = new JsonResult();
        try {
            clientService.changeClientState(client);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("客户移动失败,请重试");
        }
        return jsonResult;
    }

    /**
     * 获取对应id 的客户信息
     * @param id
     * @return
     */
    @RequiresPermissions("client:get")
    @PermissionName("获取一客户")
    @RequestMapping("get")
    @ResponseBody
    public Client get(Long id) {
        return id != null?clientService.get(id):null;
    }



    /**
     * 获取对应id 的客户信息
     * @param id
     * @return
     */
    @RequiresPermissions("client:Compensated")
    @PermissionName("获取已赔付客户")
    @RequestMapping("Compensated")
    @ResponseBody
    public List<Client> queryForClientWhichCompensate(Long id,@ModelAttribute("keyword")String keyword) {
        return clientService.queryForClientWhichCompensate(keyword);
    }


    @RequestMapping("Compensatedlist")
    public String toClientPageWhichCompensate(Long id) {
        return "Compensatedclient/list" ;
    }




}
